//
//  MenuListTableViewCell.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/8/24.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MenuListTableViewCell.h"
#import "MenuTableViewCellModel.h"

@implementation MenuListTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.iconImageView = [[UIImageView alloc] init];
        self.iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.iconImageView fixedSize:CGSizeMake(30, 30)];
        [self.contentView addSubview:self.iconImageView];
        //
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.numberOfLines = 0;
        [self.contentView addSubview:self.titleLabel];

        self.expendButton = [[MKUIFlowLayoutButton alloc] init];
        [self.expendButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
        [self.expendButton setTitle:@"^" forState:UIControlStateNormal];
        [self.expendButton setTitle:@"v" forState:UIControlStateSelected];
        [self.expendButton addTarget:self action:@selector(__onExpendButtonChange:) forControlEvents:UIControlEventTouchUpInside];
        MKUILayoutConstraintItemWrapper *w3 = [MKUILayoutConstraintItemWrapper wrapItem:self.expendButton fixedSize:CGSizeMake(44, 44)];
        [self.contentView addSubview:self.expendButton];
//
        self.childrenMenuView = [[MenuListView alloc] init];
        [self.contentView addSubview:self.childrenMenuView];
        //
        
        MKUIFlowLayoutConstraint *flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,self.titleLabel,w3] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:10];
        self.childflowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[flowlayout,self.childrenMenuView] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_L) contentInsets:UIEdgeInsetsMake(10, 20, 10, 20) interitemSpacing:10];
        //
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.childflowlayout.bounds = bounds;
    [self.childflowlayout layoutItemsWithResizeItems:YES];
}
- (void)__onExpendButtonChange:(id)sender{
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    cm.expendChildren = !cm.expendChildren;
    [cm refreshWithAnimated:YES];
}
- (MenuListTableViewCellModel *)menuListCellModel{
    return (MenuListTableViewCellModel *)self.cellModel;
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    Menu *modelObject = self.cellModel.modelValue;
    self.iconImageView.image = modelObject.icon;
    self.titleLabel.text = modelObject.title;
    
    MenuListTableViewCellModel *cm = self.menuListCellModel;
    self.expendButton.selected = cm.expendChildren;
    self.childrenMenuView.hidden = !cm.expendChildren;
    if(cm.expendChildren){
        [self __reloadChildrenData];
    }
}
- (void)__reloadChildrenData{
    self.childrenMenuView.menuList = self.menuListCellModel.childrenMenus;
}
- (CGSize)customSizeThatFits:(CGSize)size{//动态高度
    return [self.childflowlayout sizeThatFits:size resizeItems:YES];
}
@end


@implementation MenuSubTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.flowlayout.contentInsets = UIEdgeInsetsMake(5, 40, 5, 20);
    }
    return self;
}
//- (CGSize)sizeThatFits:(CGSize)size{
//    return CGSizeMake(size.width, 40);
//}
@end
